//
//  LivestockMapBottomView.m
//  Trackit
//
//  Created by yaodaobin on 2017/4/13.
//  Copyright © 2017年 yaodaobin. All rights reserved.
//

#import "LivestockMapBottomView.h"
#import "MapBottomModel.h"
#import "MapBottomSingleButton.h"

@interface LivestockMapBottomView()

@property (nonatomic, strong) NSArray *array;

@end


@implementation LivestockMapBottomView

- (instancetype)initWithFrame:(CGRect)frame withTaskDtail:(TaskDetailModel *)model array:(NSArray *)tempArray {
    self = [super initWithFrame:frame];
    if (self) {
        self.taskDetail = model;
        self.array = [NSArray arrayWithArray:tempArray];
        [self initUI];
    }
    return self;
}

- (void)initUI {
    self.axis = UILayoutConstraintAxisHorizontal;
    self.alignment = UIStackViewAlignmentCenter;
    self.distribution = UIStackViewDistributionFillEqually;
    [self createUI];
}

- (void)createUI {
    self.spacing = (kScreenWidth - self.array.count * 40) / (self.array.count + 1);
    kWeakSelf
    for (int i = 0; i < self.array.count; i++) {
        MapBottomModel *model = self.array[i];
        MapBottomSingleButton *button = [[MapBottomSingleButton alloc] initWithFrame:CGRectZero image:model.image text:model.title];
        button.tag = i;
        button.tapBlock = ^(NSInteger index){
            if (weakSelf.block) {
                weakSelf.block(index);
            }
        };
        [self addArrangedSubview:button];
    }
}

- (void)resetSubviews:(NSArray *)array {
    self.array = [NSMutableArray arrayWithArray:array];
    for (UIView *view in self.arrangedSubviews) {
        [view removeFromSuperview];
    }
    
    [self createUI];
}

@end
